#include "device_manage.h"
#include "voice_task.h"
#include <process.h>
#include <etimer.h>
#include <string.h>
#include "uart.h"
#include "74hc595.h"
#include "time.h"
#include "stm32f1xx_hal.h"   
#include "fifo.h"   
#include "tm1804.h"


static uint8_t voice_value = 0;
							 
PROCESS(VoicePlay_Process, "VoiceCombined");
PROCESS(NVCPlay_Process, "PlayVoice");
							 
							 
PROCESS_THREAD(VoicePlay_Process, ev, data)
{
	static sVoiceCmd voice;
	static struct etimer et;		
	
	PROCESS_BEGIN();
	
	voice = *((sVoiceCmd*)data);
		
	HAL_GPIO_WritePin(NVC_SD_GPIO_Port, NVC_SD_Pin, GPIO_PIN_RESET);
	etimer_set(&et,50);
	PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
	
	if(voice.cmd == voicecmd_none){
		etimer_set(&et,200);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		voice_value = *((uint8_t*)voice.value);
		voice_value = 0xE0 + voice_value - 1;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,100);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
	}else if(voice.cmd == voicecmd_setvolume){
		voice_value = *((uint8_t*)voice.value);
		voice_value = 0xE0 + voice_value - 1;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,100);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = voice_ringpress;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
	}else if(voice.cmd == voicecmd_playone){
		voice_value = *((uint8_t*)voice.value);
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
//		etimer_set(&et,3500);
//		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
	}else if(voice.cmd == voicecmd_playcall){
		static uint16_t num = 0;
		num = *((uint16_t*)voice.value);
		voice_value = cfg_data.Val.BellIndex + voice_ringdingdong -1;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,700);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = num%1000/100;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,600);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = num%100/10;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,600);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = num%10/1;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,700);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = voice_call;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,500);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
	}else if(voice.cmd == voicecmd_playaddnum){
		voice_value = *((uint16_t*)voice.value)%1000/100;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,500);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = *((uint16_t*)voice.value)%100/10;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,500);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = *((uint16_t*)voice.value)%10/1;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,700);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
		
		voice_value = voice_addsuccess;
		process_post(&NVCPlay_Process, PROCESS_EVENT_MSG, &voice_value);
		etimer_set(&et,500);
		PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
	}
//	etimer_set(&et,500);
//	PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
//	HAL_GPIO_WritePin(NVC_SD_GPIO_Port, NVC_SD_Pin, GPIO_PIN_SET);	
	PROCESS_END();
}							 
							 

PROCESS_THREAD(NVCPlay_Process, ev, data)
{
	PROCESS_BEGIN();
	static struct etimer et;	
	static uint8_t* addr = NULL;
	static uint8_t i = 0;
	
	static sVoiceCmd voice;
	static uint8_t Volume;
	Volume = cfg_data.Val.Volume;
	voice.cmd 	= voicecmd_none;
	voice.value = (uint8_t*)&Volume;
	process_start(&VoicePlay_Process, (process_data_t)&voice);
	
	while(1){
		PROCESS_WAIT_EVENT();
		if(ev == PROCESS_EVENT_MSG){
			addr = data;
			etimer_set(&et,1);
			PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
			HAL_GPIO_WritePin(NVC_GPIO_Port, NVC_Pin, GPIO_PIN_RESET);
			etimer_set(&et,3);
			PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
			for(i = 0; i<8; i++){
				HAL_GPIO_WritePin(NVC_GPIO_Port, NVC_Pin, GPIO_PIN_SET);
				if(*addr&0x01){
//					etimer_set(&et,3);
//					PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
					clock_wait(3);
					HAL_GPIO_WritePin(NVC_GPIO_Port, NVC_Pin, GPIO_PIN_RESET);
//					etimer_set(&et,1);
//					PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
					clock_wait(1);
				}else{
//					etimer_set(&et,1);
//					PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
					clock_wait(1);
					HAL_GPIO_WritePin(NVC_GPIO_Port, NVC_Pin, GPIO_PIN_RESET);
//					etimer_set(&et,3);
//					PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
					clock_wait(3);
				}
				(*addr)>>=1;
			}
			HAL_GPIO_WritePin(NVC_GPIO_Port, NVC_Pin, GPIO_PIN_SET);
		}
	}
	PROCESS_END();
}
